package pe.gob.foncodes.ssa.bs.common.util;

public class ConversionHelperThreadSafety {

    private Integer anchoBanda;

    public ConversionHelperThreadSafety(Integer anchoBanda) {
        this.anchoBanda = anchoBanda;
    }
    public final static int CONVERSION_UNIT = 1024;
    public final static int CONVERSION_LIMIT = 10000;
    public final static int CONVERSION_UNIT_MILES = 1000;

    public Long bitsToKiloBytes(Long bits) {
        Long result = bits / (CONVERSION_UNIT * 8L);
        return result;
    }

    public Long kiloBytesToBits(Long kilobytes) {
        Long result = kilobytes * CONVERSION_UNIT * 8L;
        return result;
    }

    public Integer bytesToKiloBytesMarketing(Integer bytes) {
        Integer result = bytes / CONVERSION_UNIT_MILES;
        return result;
    }

    public Integer kiloBytesToBytesMarketing(Integer kilobytes) {
        Integer result = kilobytes * CONVERSION_UNIT_MILES;
        return result;
    }

    public Float kiloBytesToMbMarketing(Integer kilobytes) {
        Float result = ((float) kilobytes) / CONVERSION_UNIT;
        if (anchoBanda >= CONVERSION_LIMIT) {
            result = ((float) kilobytes) / CONVERSION_UNIT_MILES;
        } else {
            result = ((float) kilobytes) / CONVERSION_UNIT;
        }
        return result;
    }

    public String kiloBytesToMarketingStringWithSufix(Integer kilobytes) {
        return kiloBytesToMarketingString(kilobytes, true, false);
    }

    public String kiloBytesToMarketingStringWithOutSufix(Integer kilobytes) {
        return kiloBytesToMarketingString(kilobytes, false, false);
    }

    public String kiloBytesToMarketingStringWithSufix(Integer kilobytes, Integer velocidadDescarga) {
        String temp = "";
        if (kilobytes != null && velocidadDescarga != null) {
            if (velocidadDescarga < CONVERSION_LIMIT) {
                temp = kiloBytesToMarketingString(kilobytes, true, false);
            } else {
                temp = kiloBytesToMarketingString(kilobytes, true, true);
            }
        } else {
            temp = kiloBytesToMarketingString(kilobytes, true, false);
        }
        return temp;
    }

    public String kiloBytesToMarketingStringWithOutSufix(Integer kilobytes, Integer velocidadDescarga) {
        String temp = "";
        if (kilobytes != null && velocidadDescarga != null) {
            if (velocidadDescarga < CONVERSION_LIMIT) {
                temp = kiloBytesToMarketingString(kilobytes, false, false);
            } else {
                temp = kiloBytesToMarketingString(kilobytes, false, true);
            }
        } else {
            temp = kiloBytesToMarketingString(kilobytes, true, false);
        }
        return temp;
    }

    public String kiloBytesToMarketingString(Integer kilobytes, Boolean concatSuffix, Boolean forceMilesConversion) {
        Boolean ceroMark = false;
        if (concatSuffix != null) {
            ceroMark = concatSuffix;
        }
        String result = "";
        if (kilobytes != null) {
            if (!forceMilesConversion) {
                if (anchoBanda < CONVERSION_LIMIT) {
                    if (kilobytes >= CONVERSION_UNIT) {
                        if (kilobytes % CONVERSION_UNIT == 0) {
                            if (concatSuffix) {
                                result = kiloBytesToMbMarketing(kilobytes).intValue() + "M";
                            } else {
                                result = String.valueOf(kiloBytesToMbMarketing(kilobytes).intValue());
                            }
                        } else {
                            if (concatSuffix) {
                                result = kilobytes.toString().concat("K");
                            } else {
                                result = kilobytes.toString();
                            }
                        }
                    } else {
                        if (concatSuffix) {
                            result = kilobytes.toString().concat("K");
                        } else {
                            result = kilobytes.toString();
                        }
                    }
                } else {
                    if (kilobytes >= CONVERSION_UNIT_MILES) {
                        if (kilobytes % CONVERSION_UNIT_MILES == 0) {
                            if (concatSuffix) {
                                result = kiloBytesToMbMarketing(kilobytes).intValue() + "M";
                            } else {
                                result = String.valueOf(kiloBytesToMbMarketing(kilobytes).intValue());
                            }
                        } else {
                            if (concatSuffix) {
                                result = kilobytes.toString().concat("K");
                            } else {
                                result = kilobytes.toString();
                            }
                        }
                    } else {
                        if (concatSuffix) {
                            result = kilobytes.toString().concat("K");
                        } else {
                            result = kilobytes.toString();
                        }
                    }
                }
            } else {
                if (kilobytes >= CONVERSION_UNIT_MILES) {
                    if (kilobytes % CONVERSION_UNIT_MILES == 0) {
                        if (concatSuffix) {
                            result = kiloBytesToMbMarketing(kilobytes).intValue() + "M";
                        } else {
                            result = String.valueOf(kiloBytesToMbMarketing(kilobytes).intValue());
                        }
                    } else {
                        if (concatSuffix) {
                            result = kilobytes.toString().concat("K");
                        } else {
                            result = kilobytes.toString();
                        }
                    }
                } else {
                    if (concatSuffix) {
                        result = kilobytes.toString().concat("K");
                    } else {
                        result = kilobytes.toString();
                    }
                }
            }
        } else {
            result = "0K";
        }
        return result;
    }

    public static double Redondear(double numero, int digitos) {
        int cifras = (int) Math.pow(10, digitos);
        return Math.rint(numero * cifras) / cifras;
    }
}
